#include"list.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
Status InitList_L(LinkList& L)
{
	L = new Lnode;
	L->next = NULL;
	return OK;
}
int IsEmpty(LinkList L)
{
	if (L->next)
		return 0;
	return 1;
}
void Destroy(LinkList& L)
{
	while (L)
	{
		LinkList p = L;
		L = L->next;
		delete p;
	}
};
Status ClearList(LinkList& L)
{
	LinkList p, q;
	p = L->next;
	while (p)
	{
		q = p->next;
		delete p;
		p = q;
	}
	L->next = NULL;
	return OK;
}
int GetLength(LinkList L)
{
	LinkList p = L->next;
	int i = 0;
	while (p)
	{
		++i;
		p = p->next;
	}
	return i;
}
int GetElem(LinkList L, int i, ElemType& e)
{
	LinkList p = L->next;
	int j = 1;
	while (p && j < i)
	{
		p = p->next;
		++j;
	}
	if (j > i || !p)return ERROR;
	e = p->data;
	return OK;

}

Lnode* LocateElem(LinkList L, ElemType e)
{
	LinkList p = L->next;
	while (p && p->data != e)
	{
		p = p->next;
	}
	return p;
}
Status ListInsert_L(LinkList& L, int i, ElemType e)
{
	LinkList p = L,s;
	int j = 0;
	while (p && j < i - 1)
	{
		p = p->next;
		j++;
	}
	if (!p || j > i - 1)return ERROR;
	s = new Lnode; s->data = e;
	s->next = p->next;
	p->next = s;
	return OK;
}
Status ListDelete_L(LinkList& L, int i)
{
	LinkList p = L, q;
	int j = 0;
	while (p->next && j < i - 1)
	{
		p = p->next;
		++j;
	}
	if (!p->next || j > i - 1)return ERROR;
	q = p->next;
	p->next = q->next;
	delete q;
	return OK;
}
void CreateList_H(LinkList& L, int n)
{
	L = new Lnode;
	L->next = NULL;
	for (int i = n; i > 0; --i)
	{
		LinkList p = new Lnode;
		cin >> p->data;
		p->next = L->next;
		L->next = p;
	}
}
void CreateList_T(LinkList& L, int n)
{
	L = new Lnode;
	L->next = NULL;
	LinkList r = L;
	for (int i = 0; i < n; ++i)
	{
		LinkList p = new Lnode;
		cin >> p->data;
		p->next = NULL;
		r->next = p;
		r = p;
	}
}
void MergeList(LinkList& La, LinkList& Lb, LinkList& Lc)
{
	LinkList pa, pb, pc;
	pa = La->next; pb = Lb->next;
	pc = Lc = La;
	while (pa&&pb)
	{
		if (pa->data < pb->data)
		{
			pc->next = pa;
			pc = pa;
			pa = pa->next;
		}
		else
		{
			pc->next = pb;
			pc = pb;
			pb = pb->next;
		}
	}
	pc->next = pa ? pa : pb;
	delete Lb;
}
